package practise.SudokuChecker;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Bate1_1
{
	private static BufferedInputStream in = new BufferedInputStream(System.in);
	
	public static void main(String[] args) throws IOException
	{
		int n = readInt()+1;
		for(int i=1; i<n; i++)
		{
			handel(i);
		}
	}
	
	private static void handel(int c) throws IOException
	{
		int n = readInt();
		int s = n*n;
		
		int[][] nums = new int[s][s];
		
		for(int i=0; i<s; i++)
		{
			for(int j=0; j<s; j++)
			{
				nums[i][j] = readInt();
			}
		}
		
		for(int i=0; i<s; i++)
		{
			int[] s1 = new int[s];
			int[] s2 = new int[s];
			int[] s3 = new int[s];
			
			for(int j=0; j<s; j++)
			{	
				s1[j] = nums[j][i];
				s2[j] = nums[i][j];
				s3[j] = nums[i/n*n+j/n][i%n*n+j%n];
			}
			
			if(check(s1) || check(s2) || check(s3))
			{
				System.out.println("Case #" + c + ": No");
				return;
			}
		}
		
		System.out.println("Case #" + c + ": Yes");
	}
	
	private static boolean check(int[] s)
	{
		Arrays.sort(s);
		
		for(int i=0; i<s.length; i++)
		{
			if(s[i] != i+1)
				return true;
		}

		return false;
	}

	private static int readInt() throws IOException
	{
		int i,sum=0;
		
		while(((i=in.read())&48) != 48 || i>57);
		
		for(;(i&56) == 48 || (i&62) == 56; i=in.read())
			sum = sum*10 + (i&15);
		
		return sum;
	}
}
